<?php


namespace backend\modules\tool\DataSource\Log;
use backend\modules\tool\helpers\FileHelper;

define("log_path",dirname(__DIR__)."/Filesystem/log/");

class Log
{
    protected static $object=[];
    protected $wirte_path;

    /**
     * @param $file_path
     * @return Log
     */
    public static function SingleTon($file_path){
        if(empty(self::$object[$file_path])){
            self::$object[$file_path]=new self($file_path);
        }
        return self::$object[$file_path];
    }
    protected function __construct($file_path)
    {
        FileHelper::mkdir(log_path);
        $this->wirte_path=log_path.$file_path;
    }

    public function write($message){
        echo $message.PHP_EOL;
        file_put_contents($this->wirte_path.".log",$message.PHP_EOL,FILE_APPEND|LOCK_EX);
    }
    public function getLog(){
        if(!is_file($this->wirte_path.".log")){
            return "该进程尚未运行";
        }
        $data=file_get_contents($this->wirte_path.".log");
        $data=explode("=======================>",$data);
        $data=array_reverse($data);
        $data=array_slice($data,0,200);
        return implode("=======================>",$data);
    }
//    public function __destruct()
//    {
//        $this->write("=======================>");
//    }
}